<template>
  <div class="nav">
    <el-menu
        :default-active="activeIndex"
        class="el-menu-demo"
        mode="horizontal"
        @select="handleSelect"
        background-color="#545c64"
        text-color="#fff"
        active-text-color="#ffd04b">
      <el-menu-item index="1">
        首页
      </el-menu-item>
      <el-submenu index="2" v-if="$store.getters.hasLogin">
        <template slot="title">{{ $store.state.user.username }}</template>
        <el-menu-item index="2-1">个人资料</el-menu-item>
        <el-menu-item index="2-3">我的</el-menu-item>
        <el-menu-item index="2-2">退出</el-menu-item>
      </el-submenu>
      <el-menu-item index="3" v-else>登陆</el-menu-item>
    </el-menu>
  </div>
</template>

<script>
export default {
  name: "MyNav",
  data() {
    return {
      activeIndex: '1'
    };
  },
  methods: {
    handleSelect(key, keyPath) {
      switch (key) {
        case "1":
          this.$router.push("/");
          break;
        case "2-1":
          this.$router.push("user");
          break;
        case "2-2":
          this.$store.dispatch("logout");
          this.$router.push("login");
          break;
        case "2-3":
          this.$router.push("my");
          break;
        case "3":
          this.$router.push("login");
          break;
      }
    }
  }
}
</script>

<style scoped>
.nav {
  min-width: 1200px;
}
</style>